<?
/***************************************************************************
*                            Dolphin Smart Community Builder
*                              -------------------
*     begin                : Mon Mar 23 2006
*     copyright            : (C) 2007 BoonEx Notice
*     website              : http://www.boonex.com
* This file is part of Dolphin - Smart Community Builder
*
* Dolphin is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the
* License, or  any later version.
*
* Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Dolphin,
* see license.txt file; if not, write to marketing@boonex.com
***************************************************************************/

bx_import('BxDolTwigModuleDb');

/*
 * Notices module Data
 */
class BxNoticesDb extends BxDolTwigModuleDb {	

	/*
	 * Constructor.
	 */
	function BxNoticesDb(&$oConfig) {
        parent::BxDolTwigModuleDb($oConfig);

        $this->_sTableMain = 'main';
        $this->_sTableMediaPrefix = '';
        $this->_sFieldId = 'id';
        $this->_sFieldAuthorId = 'author_id';
        $this->_sFieldUri = 'uri';
        $this->_sFieldTitle = 'title';
        $this->_sFieldDescription = 'desc';
        $this->_sFieldAnonymous = 'anonymous'; 
        $this->_sFieldTags = 'tags';
        $this->_sFieldThumb = 'thumb';
        $this->_sFieldStatus = 'status';
        $this->_sFieldFeatured = 'featured';
        $this->_sFieldCreated = 'created';
        $this->_sTableAdmins = 'admins';
        $this->_sFieldAllowViewTo = 'allow_view_notice_to';
	}
  
    function deleteEntryByIdAndOwner ($iId, $iOwner, $isAdmin) {
        if ($iRet = parent::deleteEntryByIdAndOwner ($iId, $iOwner, $isAdmin)) {
            $this->query ("DELETE FROM `" . $this->_sPrefix . "admins` WHERE `id_entry` = $iId");
            $this->deleteEntryMediaAll ($iId, 'images');
            $this->deleteEntryMediaAll ($iId, 'videos');
            $this->deleteEntryMediaAll ($iId, 'sounds');
            $this->deleteEntryMediaAll ($iId, 'files');
        }
        return $iRet;
    }

	//override forum delete since there is no forum
	function deleteForum ($iEntryId){
		//
	}
 
	function getMonth($iMonth){
		$aMonths = array(
					0=>_t('_January'),
					1=>_t('_February'),
					2=>_t('_March'),
					3=>_t('_April'),
					4=>_t('_May'),
					5=>_t('_June'),
					6=>_t('_July'),
					7=>_t('_August'),
					8=>_t('_September'),
					9=>_t('_October'),
					10=>_t('_November'),
					11=>_t('_December') 
				);

		return $aMonths[$iMonth];
	}

	function publish(&$aIds) {
		$aIds = $this->getColumn("SELECT 
				`id`
			FROM `" . $this->_sPrefix . "main`
			WHERE `status`='draft' AND `when`<=UNIX_TIMESTAMP()");
		if(empty($aIds))
			return false;

		return (int)$this->query("UPDATE `" . $this->_sPrefix . "main`
			SET `status`='approved' 
			WHERE `id` IN ('" . implode("','", $aIds) . "')") > 0;
	}
 
    function publishEntry ($iId) {
        return $this->query ("UPDATE `" . $this->_sPrefix . $this->_sTableMain . "` SET `{$this->_sFieldStatus}` = 'approved', `publish`=1 WHERE `{$this->_sFieldId}` = $iId LIMIT 1");
    } 

	function processNotices(){
		$this->processRecurring();
 	}


	function getrecurring_periods()
	{ 
 		$aOptions = array(
			'daily'=>_t('_modzzz_notices_recurring_daily'),
			'weekly'=>_t('_modzzz_notices_recurring_weekly'),
			'biweekly'=>_t('_modzzz_notices_recurring_biweekly'),
			'monthly'=>_t('_modzzz_notices_recurring_monthly'),
			'quarterly'=>_t('_modzzz_notices_recurring_quarterly'),
			'yearly'=>_t('_modzzz_notices_recurring_yearly')
		);
 
		return $aOptions;
	}

	function getRecurringCount($iStart, $iEnd, $iIncrement=1)
	{
		$aVals=array();
		$aVals[0] = _t('_modzzz_notices_recurring_unlimited'); 
		for($iter=$iStart; $iter<=$iEnd; $iter+=$iIncrement){ 
			$aVals[$iter] = $iter; 
		}

		return $aVals;
	} 
   
	function processRecurring(){
	
		$iTime = time();
 
		$aNotice = $this->getAll("SELECT `id`, `recurring_period`, `recurrence`, `recurring_num`, `start`, `end` FROM `" . $this->_sPrefix . "main` WHERE `recurring`='yes' AND `status`='approved' AND `end` <= $iTime"); 
 
 		foreach($aNotice as $aEachNotice){ 
		 
			$iNoticeId = (int)$aEachNotice['id']; 
			$sPeriod = $aEachNotice['recurring_period'];
			$iRecurrence = (int)$aEachNotice['recurrence'];
			$iRecurringNum = (int)$aEachNotice['recurring_num'];
			$iNoticeStart = (int)$aEachNotice['start'];
			$iNoticeEnd = (int)$aEachNotice['end'];
  
			if(($iRecurrence >= $iRecurringNum) && $iRecurringNum)
				continue;
  
			switch($sPeriod){ 
				case "daily":
 					$iNewStart = mktime(date("H", $iNoticeStart), date("i", $iNoticeStart), date("s", $iNoticeStart), date("n", $iNoticeStart), date("j",$iNoticeStart)+1, date("Y",$iNoticeStart));
 					$iNewEnd = mktime(date("H", $iNoticeEnd), date("i", $iNoticeEnd), date("s", $iNoticeEnd), date("n", $iNoticeEnd), date("j",$iNoticeEnd)+1, date("Y",$iNoticeEnd)); 
				break;
				case "weekly":
 					$iNewStart = mktime(date("H", $iNoticeStart), date("i", $iNoticeStart), date("s", $iNoticeStart), date("n", $iNoticeStart), date("j",$iNoticeStart)+7, date("Y",$iNoticeStart));
 					$iNewEnd = mktime(date("H", $iNoticeEnd), date("i", $iNoticeEnd), date("s", $iNoticeEnd), date("n", $iNoticeEnd), date("j",$iNoticeEnd)+7, date("Y",$iNoticeEnd)); 
				break;
				case "biweekly":
 					$iNewStart = mktime(date("H", $iNoticeStart), date("i", $iNoticeStart), date("s", $iNoticeStart), date("n", $iNoticeStart), date("j",$iNoticeStart)+14, date("Y",$iNoticeStart));
 					$iNewEnd = mktime(date("H", $iNoticeEnd), date("i", $iNoticeEnd), date("s", $iNoticeEnd), date("n", $iNoticeEnd), date("j",$iNoticeEnd)+14, date("Y",$iNoticeEnd)); 
				break;
				case "monthly":
 					$iNewStart = mktime(date("H", $iNoticeStart), date("i", $iNoticeStart), date("s", $iNoticeStart), date("n", $iNoticeStart)+1, date("j",$iNoticeStart), date("Y",$iNoticeStart));
 					$iNewEnd = mktime(date("H", $iNoticeEnd), date("i", $iNoticeEnd), date("s", $iNoticeEnd), date("n", $iNoticeEnd)+1, date("j",$iNoticeEnd), date("Y",$iNoticeEnd)); 
 				break;
				case "quarterly":
 					$iNewStart = mktime(date("H", $iNoticeStart), date("i", $iNoticeStart), date("s", $iNoticeStart), date("n", $iNoticeStart)+3, date("j",$iNoticeStart), date("Y",$iNoticeStart));
 					$iNewEnd = mktime(date("H", $iNoticeEnd), date("i", $iNoticeEnd), date("s", $iNoticeEnd), date("n", $iNoticeEnd)+3, date("j",$iNoticeEnd), date("Y",$iNoticeEnd)); 
 				break;
				case "yearly":
 					$iNewStart = mktime(date("H", $iNoticeStart), date("i", $iNoticeStart), date("s", $iNoticeStart), date("n", $iNoticeStart), date("j",$iNoticeStart), date("Y",$iNoticeStart)+1);
 					$iNewEnd = mktime(date("H", $iNoticeEnd), date("i", $iNoticeEnd), date("s", $iNoticeEnd), date("n", $iNoticeEnd)+1, date("j",$iNoticeEnd), date("Y",$iNoticeEnd)+1); 
 				break;
			}

			$this->query("UPDATE `" . $this->_sPrefix . "main` SET 
						`start`=$iNewStart,
						`end`=$iNewEnd, 
						`recurrence` = `recurrence` + 1,
						`views` = 0,
						`comments_count` = 0 
						WHERE `ID` = '$iNoticeId'
						"); 
  /*
			$aPart = $this->getAll("SELECT `id_profile` FROM `modzzz_notices_participants` WHERE `id_entry` = '$iNoticeId' AND `confirmed`=1"); 
			foreach($aPart as $aEachPart){ 
				$iRecipientID = $aEachPart['id_profile'];
				$this->SendFriendsNotification($iRecipientID, $iNoticeId, 'recurring');
			}
  */		 
 		}
	}

    function getTravelEntriesByMonth ($iYear, $iMonth, $iNextYear, $iNextMonth) {
        return $this->getAll ("SELECT *, DAYOFMONTH(FROM_UNIXTIME(`{$this->_sFieldCreated}`)) AS `Day`
            FROM `" . $this->_sPrefix . $this->_sTableMain . "`
            WHERE `{$this->_sFieldCreated}` >= UNIX_TIMESTAMP('$iYear-$iMonth-1') AND `{$this->_sFieldCreated}` < UNIX_TIMESTAMP('$iNextYear-$iNextMonth-1') AND `category`='travel' AND 
			`{$this->_sFieldStatus}` = 'approved'");
    }

    function getAvailableEntriesByMonth ($iYear, $iMonth, $iNextYear, $iNextMonth) {
        return $this->getAll ("SELECT *, DAYOFMONTH(FROM_UNIXTIME(`{$this->_sFieldCreated}`)) AS `Day`
            FROM `" . $this->_sPrefix . $this->_sTableMain . "`
            WHERE `{$this->_sFieldCreated}` >= UNIX_TIMESTAMP('$iYear-$iMonth-1') AND `{$this->_sFieldCreated}` < UNIX_TIMESTAMP('$iNextYear-$iNextMonth-1') AND `category`='available' AND `{$this->_sFieldStatus}` = 'approved'");
    }

}

?>